<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
            <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
            <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0">
            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-popconfirm
                    class="ml-5"
                    confirm-button-text='确定'
                    cancel-button-text='我再想想'
                    icon="el-icon-info"
                    icon-color="red"
                    title="您确定批量删除这些数据吗？"
                    @confirm="delBatch">
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
            <!-- <el-upload action="http://localhost:9090/order/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
              <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
            </el-upload>
            <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button> -->
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'" @expand-change="handleExpandChange"
                  @selection-change="handleSelectionChange" ref="classTable">
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-table :data="orderDetails[props.row.orderUserId]">
                        <el-table-column type="selection" width="55"></el-table-column>
                        <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
                        <el-table-column prop="orderId" label="订单号" align="center"></el-table-column>
                        <el-table-column prop="orderUserId" label="客户id" align="center" v-if="false"></el-table-column>
                        <!--                    <el-table-column prop="orderUser" label="客户" align="center"></el-table-column>-->
                        <!--                    <el-table-column prop="orderUserPhone" label="客户电话" align="center"></el-table-column>-->
                        <el-table-column prop="orderPrice" label="订单价格/元" align="center">
                            <template slot-scope="scope">
                                <span>{{ scope.row.orderPrice}}</span>
                            </template>

                        </el-table-column>
                        <el-table-column prop="createTime" label="下单时间" align="center" ></el-table-column>
                        <el-table-column prop="orderStatus" label="订单状态" align="center">
                            <template slot-scope="scope">
                                <span> {{ GLOBAL.orderStatus.find(v => v.value === scope.row.orderStatus) ? GLOBAL.orderStatus.find(v => v.value === scope.row.orderStatus).label :'无' }}</span>
                            </template>

                        </el-table-column>
                        <el-table-column prop="payStatus" label="付款状态" align="center">
                            <template slot-scope="scope">
                                <span> {{ GLOBAL.payStatus.find(v => v.value === scope.row.payStatus).label }}</span>
                            </template>

                        </el-table-column>
                        <el-table-column prop="payMethod" label="付款方式" align="center">
                            <template slot-scope="scope">
                                <span> {{ GLOBAL.payMethod.find(v => v.value === scope.row.payMethod).label }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="180" align="center">
                            <template slot-scope="scope">
                                <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
                                <el-button type="text" @click="orderDetail(scope.row)">订单详情</el-button>
                                <el-popconfirm
                                        class="ml-5"
                                        confirm-button-text='确定'
                                        cancel-button-text='我再想想'
                                        icon="el-icon-info"
                                        icon-color="red"
                                        title="您确定删除吗？"
                                        @confirm="del(scope.row.id)"
                                >
                                    <el-button type="text" slot="reference">删除</el-button>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>
            </el-table-column>
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
            <!--            <el-table-column prop="orderId" label="订单号" align="center" v-if="false"></el-table-column>-->
            <el-table-column prop="orderUserId" label="客户id" align="center" v-if="false"></el-table-column>
            <el-table-column prop="orderUser" label="客户" align="center"></el-table-column>
            <el-table-column prop="orderUserPhone" label="客户电话" align="center"></el-table-column>
            <el-table-column prop="orderUserSex" label="客户性别" align="center">
                <template slot-scope="scope">
                    <span>{{ GLOBAL.sex.find(v => v.value === scope.row.orderUserSex).label}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="orderUserLevel" label="客户等级" align="center">
                <template slot-scope="scope">
                    <span>{{ GLOBAL.customerLevel.find(v => v.value === scope.row.orderUserLevel).label}}</span>
                </template>
            </el-table-column>
            <!--            <el-table-column prop="orderPrice" label="订单价格/元" align="center"></el-table-column>-->

            <!--            <el-table-column prop="orderStatus" label="订单状态" align="center">-->
            <!--                <template slot-scope="scope">-->
            <!--                    <span> {{ GLOBAL.orderStatus.find(v => v.value === scope.row.orderStatus) ?-->
            <!--                        GLOBAL.orderStatus.find(v => v.value === scope.row.orderStatus).label :'无' }}</span>-->
            <!--                </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column prop="payStatus" label="付款状态" align="center">-->
            <!--                <template slot-scope="scope">-->
            <!--                    <span> {{ GLOBAL.payStatus.find(v => v.value === scope.row.payStatus).label }}</span>-->
            <!--                </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column prop="payMethod" label="付款方式" align="center">-->
            <!--                <template slot-scope="scope">-->
            <!--                    <span> {{ GLOBAL.payMethod.find(v => v.value === scope.row.payMethod).label }}</span>-->
            <!--                </template>-->
            <!--            </el-table-column>-->

            <!--            <el-table-column label="操作" width="180" align="center">-->
            <!--                <template slot-scope="scope">-->
            <!--                    <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>-->
            <!--                    <el-button type="text" @click="orderDetail(scope.row)">订单详情</el-button>-->
            <!--                    <el-popconfirm-->
            <!--                            class="ml-5"-->
            <!--                            confirm-button-text='确定'-->
            <!--                            cancel-button-text='我再想想'-->
            <!--                            icon="el-icon-info"-->
            <!--                            icon-color="red"-->
            <!--                            title="您确定删除吗？"-->
            <!--                            @confirm="del(scope.row.id)"-->
            <!--                    >-->
            <!--                        <el-button type="text" slot="reference">删除</el-button>-->
            <!--                    </el-popconfirm>-->
            <!--                </template>-->
            <!--            </el-table-column>-->
        </el-table>
        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[10, 20,30,40]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog title="信息" ref="dialog" :visible.sync="dialogFormVisible" width="60%" :close-on-click-modal="false">
            <el-steps :active="active" finish-status="success">
                <el-step title="选择客户"></el-step>
                <el-step title="下单"></el-step>
                <!--                <el-step title="预览报价"></el-step>-->
            </el-steps>
            <div v-show="active===1">
                <OrderStep1 ref="step1" :active="active"></OrderStep1>
            </div>
            <div v-show="active===2">
                <OrderStep2 ref="step2" :active="active" :order="orderId" :customer="customerId"></OrderStep2>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button :disabled="preStatus" style="margin-top: 12px;" @click="pre">上一步</el-button>
                <el-button v-show="active !== 2" :disabled="nextStatus" style="margin-top: 12px;" @click="next">下一步
                </el-button>
                <el-button v-show="active === 2" style="margin-top: 12px;" @click="generateOrder">确认下单</el-button>
            </div>
        </el-dialog>

        <el-dialog title="订单详情" ref="orderDetailDialog" :visible.sync="orderDialogVisible" width="60%"
                   :close-on-click-modal="false">
            <OrderDetail :orderId="orderId" :customerId="customerId" ref="OrderDetail" :key="timer"></OrderDetail>
        </el-dialog>
    </div>
</template>

<script>
    import OrderStep1 from "../components/OrderStep1";
    import OrderStep2 from "../components/OrderStep2";
    import {v4 as uuidv4} from 'uuid';
    import OrderDetail from "../components/OrderDetail";

    export default {
        name: "Order",
        data() {
            return {
                tableData: [],
                orderDetails: {},
                total: 0,
                pageNum: 1,
                pageSize: 10,
                name: "",
                form: {},
                currentClassId: '',
                //子组件重新渲染绑定的值
                timer: '',
                orderId: '',
                customerId: '',
                dialogFormVisible: false,
                //订单详情
                orderDialogVisible: false,
                multipleSelection: [],
                active: 1,
                preStatus: false,
                nextStatus: false,
                ruleForm: {
                    orderId: '',
                    orderUserId: '',
                    orderUser: '',
                    orderUserPhone: '',
                    orderStatus: '',
                    orderPrice: '',
                    payStatus: '',
                    payMethod: '',
                },
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
            }
        },
        components: {OrderStep1, OrderStep2, OrderDetail},
        created() {
            this.load()
        },
        methods: {
            load() {
                const data = {
                    "page": this.pageNum,
                    "pageSize": this.pageSize,
                }
                this.request.post("/order/queryOrders", data).then(res => {
                    //this.tableData = res.data.records
                    this.tableData = res.data
                    for (let i = 0; i < this.tableData.length; i++) {
                        let name = this.tableData[i].orderUserId
                        this.orderDetails[name]= this.tableData[i].children
                    }
                    this.total = res.data.total
                    console.log(this.orderDetails)
                })
                // this.request.post("/order/page", data).then(res => {
                //     //this.tableData = res.data.records
                //     this.tableData = res.data.data
                //     this.total = res.data.total
                // })
            },
            orderDetail(row) {
                this.orderId = row.orderId
                this.customerId = row.orderUserId
                this.timer = new Date().getTime()
                this.orderDialogVisible = true
                // this.$refs.OrderDetail.load()
            },
            pre() {
                this.active--
                this.nextStatus = false
                if (this.active < 2) {
                    this.preStatus = true
                    // this.active = 0
                }
            },
            next() {
                if (this.$refs.step1.checkValidate()) {
                    this.active++
                    this.preStatus = false
                    if (this.active >= 2) {
                        this.nextStatus = true
                    }
                }
            },
            generateOrder() {
                let orderId = uuidv4()
                //初始化传给子组件的值
                this.orderId = orderId
                this.ruleForm.orderId = orderId
                this.ruleForm.orderUserId = this.$refs.step1.customerForm.customerId
                //初始化传给子组件的值
                this.customerId = this.$refs.step1.customerForm.customerId
                this.ruleForm.orderUser = this.$refs.step1.customerForm.customerName.customerName
                this.ruleForm.orderUserPhone = this.$refs.step1.customerForm.phone
                this.ruleForm.orderPrice = this.$refs.step2.orderPrice
                this.ruleForm.orderStatus = '0'
                this.ruleForm.payMethod = this.$refs.step1.customerForm.payMethod
                this.ruleForm.payStatus = this.$refs.step1.customerForm.payStatus
                let dataArr = this.$refs.step2.tableData
                //向订单表写入
                this.request.post("/order/save", this.ruleForm).then(res => {
                    if (res.code === 200) {
                        this.$refs.step2.save(dataArr)
                        this.load()
                        this.dialogFormVisible = false
                        //清空数据
                        this.$refs.step1.customerForm = {}
                        this.$refs.step2.tableData = []
                        //初始化active=1
                        this.active = 1
                    } else {
                        return this.$message.error(res.message)
                    }
                })
            },

            save(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.request.post("/order/save", this.ruleForm).then(res => {
                            if (res.code === 200) {
                                this.$message.success(res.message)
                                this.dialogFormVisible = false
                                this.load()
                            } else {
                                this.$message.error(res.message)
                            }
                        })
                    }
                })
            },
            handleExpandChange(row,expandRows){
                const $classTable = this.$refs.classTable
                if (expandRows.length >= 1) {
                    expandRows.forEach(expandRow => {
                        if (row.orderUserId !== expandRow.orderUserId) {
                            $classTable.toggleRowExpansion(expandRow, false)
                        }
                    })
                }
                this.currentClassId = row.orderUserId
            },

            handleAdd() {
                //active=1,上一步置灰
                this.preStatus = true
                this.nextStatus = false
                this.dialogFormVisible = true
                this.form = {}
                this.ruleForm = {}
                this.$nextTick(() => {
                    if (this.$refs.img) {
                        this.$refs.img.clearFiles();
                    }
                    if (this.$refs.file) {
                        this.$refs.file.clearFiles();
                    }
                })
            },
            handleEdit(row) {
                this.ruleForm = JSON.parse(JSON.stringify(row))
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    if (this.$refs.img) {
                        this.$refs.img.clearFiles();
                    }
                    if (this.$refs.file) {
                        this.$refs.file.clearFiles();
                    }
                })
            },
            del(id) {
                this.request.delete("/order/" + id).then(res => {
                    if (res.code === 200) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            delBatch() {
                if (!this.multipleSelection.length) {
                    this.$message.error("请选择需要删除的数据")
                    return
                }
                let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
                this.request.post("/order/del/batch", ids).then(res => {
                    if (res.code === 200) {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error("批量删除失败")
                    }
                })
            },
            reset() {
                this.name = ""
                this.load()
            },
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                this.form.file = res
            },
            handleImgUploadSuccess(res) {
                this.form.img = res
            },
            download(url) {
                window.open(url)
            },
            exp() {
                window.open("http://localhost:9090/order/export")
            },
            handleExcelImportSuccess() {
                this.$message.success("导入成功")
                this.load()
            }
        }
    }
</script>


<style>
    .headerBg {
        background: #eee !important;
    }

    .el-button {
        margin: 0px 6px 12px;
    }

    .el-icon {
        font-size: 16px;
    }

    .dialog-footer {
        text-align: center;
    }
</style>
